package sqsf.util;

import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import sqsf.pojo.User;

import java.util.ArrayList;
import java.util.List;

public class UserTest {
    SqlSession session;
    UserMapper mapper;
    @Before//在所有测试方法执行之前执行
    public void before(){
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
        mapper = session.getMapper(UserMapper.class);
    }
    @After//在所有测试方法执行之后执行
    public void after(){
        //5、关闭资源
        session.close();
    }

    @Test
    public void selectByUser(){
        User user = new User();
        user.setUid(1);
        List<User> userList = mapper.selectByUser(user);
        System.out.println(userList);
    }

    @Test
    public void updateByUser(){
        User user = new User();
        user.setUid(2);
        user.setPassword("888888888");
        int i = mapper.updateByUser(user);
        System.out.println(i);
    }

    @Test
    public void insertByList(){
        List<User> userList = new ArrayList<User>();
        userList.add(new User("aaa","aaa","aaa"));
        userList.add(new User("bbb","bbb","bbb"));
        userList.add(new User("ccc","ccc","ccc"));
        mapper.insertByList(userList);
    }
}
